import easygui
import cv2
from aip import AipOcr
import os
from PIL import Image,ImageDraw,ImageFont,ImageGrab
def baidu_ocr():
    #print ('start')
    APP_ID = '10839731';
    API_KEY = '93THkmKKFHGS5inBt8ulCeGH';
    SECRET_KEY = 'EVUeKdmNuCZFyD7xqx0Pr6FLWYyZENvo'
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    image=open('t.png','rb').read()
    #print(image)
    t=client.basicGeneral(image);

    """ 如果有可选参数 """

    options = {}
    options["language_type"] = "CHN_ENG"
    options["detect_direction"] = "true"
    options["detect_language"] = "true"
    options["probability"] = "true"
    #print (t)
    #print (t['words_result'])
    sum0=''
    for i in t['words_result']:
        sum0=sum0+i['words']+'\n'
    #print (sum0)
    #easygui.msgbox(sum0,title='截图转文字(可复制)')
    os.remove('t.png')
    return sum0
while True:

    X=easygui.fileopenbox()
    #X="C:/TDDownload/bbbb1.jpg"
    print(X)

    src=cv2.imread(X)
    #print(src)
    #cv2.namedWindow('input_image', cv2.WINDOW_AUTOSIZE)
    x,y,z = src.shape
    if x>y:
        src=cv2.transpose(src)
        src = cv2.flip(src, 0)
        y,x=x,y

    res=cv2.resize(src,(1200,int(1200*(x/y))),interpolation=cv2.INTER_CUBIC)
    cv2.imshow('ROI selector', res)
    #cv2.namedWindow("ROI selector", cv2.WND_PROP_FULLSCREEN)
    #cv2.setWindowProperty("ROI selector",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    r = cv2.selectROI(res)
    #print(r)
    imCrop = res[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
    cv2.imwrite('t.png',imCrop)
    name0=baidu_ocr()
    print('名称:',name0)
    r=cv2.selectROI(res)
    imCrop = res[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
    cv2.imwrite('t.png',imCrop)
    date0=baidu_ocr()
    print('日期:',date0)
    msg = "Enter your personal information"
    title = "Credit Card Application"
    fieldNames = ["Name","Date"]
    fieldValues1 = [name0,date0]  # we start with blanks for the values
    fieldValues = easygui.multenterbox(msg,title, fieldNames,fieldValues1)
    print(fieldValues)
    #w=rotate(src,90)
    #cv2.imshow('input_image2', w)
    #cv2.waitKey(0)
    cv2.destroyAllWindows()
    filename=fieldValues[0].replace('\n','')+'-'+fieldValues[1].replace('\n','')+'.JPG'
    print(filename)
    os.rename(X,filename)
